package com.flyduck.mall.ums.dubbo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.flyduck.mall.common.core.utils.BeanUtils;
import com.flyduck.mall.ums.api.RemoteUserService;
import com.flyduck.mall.ums.api.vo.RemoteUserRespVO;
import com.flyduck.mall.ums.api.vo.RemoteUserSaveReqVO;
import com.flyduck.mall.ums.api.vo.RemoteUserUpdateReqVO;
import com.flyduck.mall.ums.domain.UmsUser;
import com.flyduck.mall.ums.mapper.UmsUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * UmsUserApiServiceImpl
 * </p>
 *
 * @author flyduck
 * @since 2024-07-10
 */
@Slf4j
@DubboService
public class RemoteUserServiceImpl implements RemoteUserService {

    @Resource
    private UmsUserMapper umsUserMapper;

    @Override
    public RemoteUserRespVO getUserByUsername(String username) {
        UmsUser appUser = umsUserMapper.selectOne(
                new LambdaQueryWrapper<UmsUser>()
                        .eq(UmsUser::getUsername, username)
        );
        RemoteUserRespVO remoteUserRespVO = BeanUtils.toBean(appUser, RemoteUserRespVO.class);
        return remoteUserRespVO;
    }

    @Override
    public void saveUser(RemoteUserSaveReqVO remoteUserSaveReqVO) {
        UmsUser appUser = BeanUtils.toBean(remoteUserSaveReqVO, UmsUser.class);
        umsUserMapper.insert(appUser);
    }

    @Override
    public void updateUser(RemoteUserUpdateReqVO remoteUserUpdateReqVO) {
        UmsUser appUser = BeanUtils.toBean(remoteUserUpdateReqVO, UmsUser.class);
        umsUserMapper.updateById(appUser);
    }

    @Override
    public RemoteUserRespVO getUserById(Long id) {
        UmsUser appUser = umsUserMapper.selectById(id);
        RemoteUserRespVO remoteUserRespVO = BeanUtils.toBean(appUser, RemoteUserRespVO.class);
        return remoteUserRespVO;
    }
}
